/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.service.UserService;
import org.zkoss.zhtml.Form;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Administrator
 */
public class Login extends GenericAutowireComposer {

    private UserService userService = util.Utility.getUtility().getUserService();
    private Form loginForm;
    private Textbox j_username;
    private Textbox j_password;
    private Label mesgLbl;

    public void onLoadForm(Event event) {
        j_username.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateUsername(cmpnt, o);
            }
        });
        j_password.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validatePassword(cmpnt, o);
            }
        });
    }

    public void onSubmitLogin(Event e) {
        //System.out.println(j_username.getValue());
        //System.out.println(j_password.getValue());


        if (userService.findUser(j_username.getValue()) == null) {
            mesgLbl.setValue("User Name or Password Invalid!");
            Clients.evalJavaScript("loginFaild()");
            return;
        } else {
            if (org.zkoss.spring.security.SecurityUtil.isAnyGranted("Adminstrative")) {
                Executions.sendRedirect("/adminStaff/home.do");
            } else if (org.zkoss.spring.security.SecurityUtil.isAnyGranted("Doctor")) {
                Executions.sendRedirect("/doctor/home.do");
            } else if (org.zkoss.spring.security.SecurityUtil.isAnyGranted("Laboratory")) {
                Executions.sendRedirect("/laboratory/home.do");
            } else {
                Executions.sendRedirect("/home.do");
            }
             Clients.submitForm(loginForm);
        }
       
    }

    public void validateUsername(Component comp, Object value)
            throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Please enter username");
        }
        if (!s.matches("^[a-zA-Z][\\w]{3,19}$")) {
            throw new WrongValueException(comp,
                    "Username must be 3-20 alphanumeric characters");
        }
    }

    public void validatePassword(Component comp, Object value)
            throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Please enter password");
        }
        if (!s.matches("^\\w{3,20}$")) {
            throw new WrongValueException(comp,
                    "Password must be 3-20 alphanumeric characters");
        }
    }
}
